package practicaaa4;

import java.math.*;


public class PracticaAA4 {

    public static void main(String[] args) {
        int c[] = {0, 1, 4, 2, 6, 7};
        int f[] = {3, 5, 6, 7, 8, 9};
        
        int b [] = {2, 4, 99, 7, 99 , 2};
        int p[] = actCompatibles(c, f);
        System.out.println(sel(c,f,b,p,c.length-1));
    }
    
    private static int [] actCompatibles (int[] c, int[] f) {
        int[] p = new int[c.length];
        
        for (int i = c.length-1; i >= 0; i--) {
            p[i] = -1;
            boolean exito = false;        
            for (int j = i-1; j>= 0 && !exito; j--) {            
                if (c[i] >= f[j]) {
                    exito = true;
                    p[i] = j;
                }
            }
        }
        
        return p;
    }
    
    public static int sel (int[] c, int[] f, int[] b, int[] p, int i) {
        
        if (i == -1) 
            return 0;
        else
            
            return Math.max(sel(c, f ,b, p, i-1), b[i] + sel(c, f, b, p, p[i]));
        
        
    }
    
}
